package com.chd.ledger_server.controller;

import com.chd.ledger_server.entity.PipeWarning;
import com.chd.ledger_server.service.PipeWarningService;
import com.chd.ledger_server.domain.PageDto;
import com.chd.ledger_server.domain.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 管材预警管理表(PipeWarning)表控制层
 *
 * @author makejava
 * @since 2025-07-06 18:51:06
 */
@RestController
@RequestMapping("/pipeWarning")
@CrossOrigin
public class PipeWarningController {
    /**
     * 服务对象
     */
    @Resource
    private PipeWarningService pipeWarningService;

    /**
     * 分页查询
     *
     * @param pageDto 分页请求实体，仅包含PipeWarning实体+分页数据
     * @return PipeWarning列表
     */
    @PostMapping("/queryByPage")
    public Result queryByPage(@RequestBody PageDto pageDto) {
        PageInfo list = pipeWarningService.queryAllPipeWarningsByPage(pageDto);
        if (list.getList().isEmpty()) return Result.buildFail("未查到相关信息！");
        return Result.buildSuccess(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryById")
    public Result queryById(Integer id) {
        PipeWarning entity = this.pipeWarningService.queryById(id);
        if (entity == null) {
            return Result.buildFail("未找到记录");
        }
        return Result.buildSuccess(entity);
    }

    /**
     * 新增数据
     *
     * @param pipeWarning 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public Result add(@RequestBody PipeWarning pipeWarning) {
        System.out.println(pipeWarning);
        PipeWarning result = this.pipeWarningService.insert(pipeWarning);
        return Result.buildSuccess(result);
    }

    /**
     * 编辑数据
     *
     * @param pipeWarning 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody PipeWarning pipeWarning) {
        PipeWarning result = this.pipeWarningService.update(pipeWarning);
        if (result == null) {
            return Result.buildFail("更新失败，记录不存在");
        }
        return Result.buildSuccess(result);
    }

    /**
     * 删除数据
     *
     * @param pipeWarId 主键
     * @return 删除是否成功
     */
    @GetMapping("/deleteById")
    public Result deleteById( Integer pipeWarId) {
        boolean success = this.pipeWarningService.deleteById(pipeWarId);
        if (success) {
            return Result.buildSuccess("删除成功");
        } else {
            return Result.buildFail("删除失败，记录不存在");
        }
    }
}
